﻿/*
 * copyright (c) 2009 hvbird
 *
 * This file is part of tdlib.
 *
 * tdlib is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * tdlib is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;

namespace TDLib.Web
{
    public class ClientScript
    {
#if NET_2_0
        /// <summary>
        /// 服务器端弹出alert对话框
        /// </summary>
        /// <param name="str_Message">提示信息,例子："请输入您姓名!"</param>
        /// <param name="page">Page类</param>
        public static void Alert(string str_Message, Page page)
        {
            ClientScriptManager cs = page.ClientScript;

            cs.RegisterStartupScript(page.GetType(), page.ToString(), "<script>alert('" + str_Message + "');</script>", false);
        }

        /// <summary>
        /// 提示并返回前一页
        /// </summary>
        /// <param name="str_Message"></param>
        /// <param name="page"></param>
        public static void AlertAndBack(string str_Message, Page page)
        {
            ClientScriptManager cs = page.ClientScript;

            cs.RegisterStartupScript(page.GetType(), page.ToString(), "<script>alert('" + str_Message + "');history.back(-1);</script>", false);
        }

        /// <summary>
        /// 提示并跳转
        /// </summary>
        /// <param name="str_Message"></param>
        /// <param name="strUrl"></param>
        /// <param name="page"></param>
        public static void AlertAndJump(string str_Message, string strUrl, Page page)
        {
            ClientScriptManager cs = page.ClientScript;

            cs.RegisterStartupScript(page.GetType(), page.ToString(), "<script>alert('" + str_Message + "');location.href='" + strUrl + "';</script>", false);
        }
        /// <summary>
        /// 服务器端弹出alert对话框
        /// </summary>
        /// <param name="str_Ctl_Name">获得焦点控件Id值,比如：txt_Name</param>
        /// <param name="str_Message">提示信息,例子："请输入您姓名!"</param>
        /// <param name="page">Page类</param>
        public static void Alert(string str_Ctl_Name, string str_Message, Page page)
        {
            ClientScriptManager cs = page.ClientScript;

            cs.RegisterStartupScript(page.GetType(), page.ToString(), "<script>alert('" + str_Message + "');document.forms(0)." + str_Ctl_Name + ".focus(); document.forms(0)." + str_Ctl_Name + ".select();</script>", false);
        }
        /// <summary>
        /// 服务器端弹出confirm对话框,该函数有个弊端,必须放到响应事件的最后,目前没有妥善解决方案
        /// </summary>
        /// <param name="str_Message">提示信息,例子："您是否确认删除!"</param>
        /// <param name="btn">隐藏Botton按钮Id值,比如：btn_Flow</param>
        /// <param name="page">Page类</param>
        public static void Confirm(string str_Message, string btn, Page page)
        {
            ClientScriptManager cs = page.ClientScript;

            cs.RegisterStartupScript(page.GetType(), page.ToString(), "<script> if (confirm('" + str_Message + "')==true){document.forms(0)." + btn + ".click();}</script>", false);
        }
        /// <summary>
        ///  服务器端弹出confirm对话框,询问用户准备转向相应操作，包括“确定”和“取消”时的操作
        /// </summary>
        /// <param name="str_Message">提示信息，比如："成功增加数据,单击\"确定\"按钮填写流程,单击\"取消\"修改数据"</param>
        /// <param name="btn_Redirect_Flow">"确定"按钮id值</param>
        /// <param name="btn_Redirect_Self">"取消"按钮id值</param>
        /// <param name="page">Page类</param>
        public static void Confirm(string str_Message, string btn_Redirect_Flow, string btn_Redirect_Self, Page page)
        {
            ClientScriptManager cs = page.ClientScript;

            cs.RegisterStartupScript(page.GetType(), page.ToString(), "<script> if (confirm('" + str_Message + "')==true){document.forms(0)." + btn_Redirect_Flow + ".click();}else{document.forms(0)." + btn_Redirect_Self + ".click();}</script>", false);
        }
        /// <summary>
        /// 刷新某个桢
        /// </summary>
        /// <param name="Frame">桢名称</param>
        /// <param name="url">连接</param>
        /// <param name="page">page对象</param>
        public static void ReloadFrame(string Frame, string url, Page page)
        {
            ClientScriptManager cs = page.ClientScript;

            cs.RegisterStartupScript(page.GetType(), page.ToString(), "<script language=\"javascript\">parent.frames('" + Frame + "').document.location.href='" + url + "';</script>", false);
        }
#endif
#if NET_1_1
        /// <summary>
        /// 服务器端弹出alert对话框
        /// </summary>
        /// <param name="str_Message">提示信息,例子："请输入您姓名!"</param>
        /// <param name="page">Page类</param>
        public static void Alert(string str_Message, Page page)
        {
            page.RegisterStartupScript("", "<script>alert('" + str_Message + "');</script>");
        }

        public static void AlertAndBack(string str_Message, Page page)
        {
            page.RegisterStartupScript("", "<script>alert('" + str_Message + "');history.back(-1);</script>");
        }

        /// <summary>
        /// 服务器端弹出alert对话框
        /// </summary>
        /// <param name="str_Message">提示信息,例子："请输入您姓名!"</param>
        /// <param name="page">Page类</param>
        public static void AlertAndJump(string str_Message, string strUrl, Page page)
        {
            page.RegisterStartupScript("", "<script>alert('" + str_Message + "');location.href='" + strUrl + "';</script>");
        }
        /// <summary>
        /// 服务器端弹出alert对话框
        /// </summary>
        /// <param name="str_Ctl_Name">获得焦点控件Id值,比如：txt_Name</param>
        /// <param name="str_Message">提示信息,例子："请输入您姓名!"</param>
        /// <param name="page">Page类</param>
        public static void Alert(string str_Ctl_Name, string str_Message, Page page)
        {
            page.RegisterStartupScript("", "<script>alert('" + str_Message + "');document.forms(0)." + str_Ctl_Name + ".focus(); document.forms(0)." + str_Ctl_Name + ".select();</script>");
        }
        /// <summary>
        /// 服务器端弹出confirm对话框,该函数有个弊端,必须放到响应事件的最后,目前没有妥善解决方案
        /// </summary>
        /// <param name="str_Message">提示信息,例子："您是否确认删除!"</param>
        /// <param name="btn">隐藏Botton按钮Id值,比如：btn_Flow</param>
        /// <param name="page">Page类</param>
        public static void Confirm(string str_Message, string btn, Page page)
        {
            page.RegisterStartupScript("", "<script> if (confirm('" + str_Message + "')==true){document.forms(0)." + btn + ".click();}</script>");
        }
        /// <summary>
        ///  服务器端弹出confirm对话框,询问用户准备转向相应操作，包括“确定”和“取消”时的操作
        /// </summary>
        /// <param name="str_Message">提示信息，比如："成功增加数据,单击\"确定\"按钮填写流程,单击\"取消\"修改数据"</param>
        /// <param name="btn_Redirect_Flow">"确定"按钮id值</param>
        /// <param name="btn_Redirect_Self">"取消"按钮id值</param>
        /// <param name="page">Page类</param>
        public static void Confirm(string str_Message, string btn_Redirect_Flow, string btn_Redirect_Self, Page page)
        {
            page.RegisterStartupScript("", "<script> if (confirm('" + str_Message + "')==true){document.forms(0)." + btn_Redirect_Flow + ".click();}else{document.forms(0)." + btn_Redirect_Self + ".click();}</script>");
        }
        /// <summary>
        /// 刷新某个桢
        /// </summary>
        /// <param name="Frame">桢名称</param>
        /// <param name="url">连接</param>
        /// <param name="page">page对象</param>
        public static void ReloadFrame(string Frame, string url, Page page)
        {
            page.RegisterStartupScript("", "<script language=\"javascript\">parent.frames('" + Frame + "').document.location.href='" + url + "';</script>");
        }
#endif
    }
}
